package net.java.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.java.action.comun.BaseAction;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.inject.Scope;
import com.opensymphony.xwork2.inject.Scoped;

import dawi.fcs.dominio.Expediente;
import dawi.fcs.dominio.dto.DtoExpediente;
import dawi.fcs.servicio.generico.UIMantenimientoController;
import dawi.fcs.servicio.generico.UValidador;
import dawi.fcs.servicio.util.MyBatisUtil;

@SuppressWarnings("serial")
@ParentPackage(value = "DAWI-FCS-WEB")
@Scoped(value = Scope.SESSION)
public class ConsultaExpedienteAction extends BaseAction implements
		UIMantenimientoController {
	private static final Logger logger = Logger
			.getLogger(ConsultaExpedienteAction.class);

	// FILTROS y LISTAS
	private Expediente filtro;
	private DtoExpediente registroSeleccionado;
	private List<DtoExpediente> lstExpedientesModel;

	// Reporte
	private String fileName, extension;
	private InputStream documento;

	public ConsultaExpedienteAction() {
		logger.debug("destructor");
		filtro = new Expediente();
	}

	@Override
	@Action(value = "/iniciarCConsultaExp", results = { @Result(name = "success", location = "consulta_expediente_listado", type = "tiles") })
	public String iniciarControladora() throws Exception {
		inicializarFiltrosListado();
		return SUCCESS;
	}

	@Override
	@Action(value = "/buscaExpedienteConsulta", results = { @Result(name = "success", location = "consulta_expediente_listado", type = "tiles") })
	public String buscar() throws Exception {
		logger.debug("Consultando...");
		HashMap<String, Object> parametros = new HashMap<String, Object>();

		if (UValidador.estaVacio(filtro.getNro_expediente()))
			filtro.setNro_expediente(" ");

		parametros.put("nro_expediente", filtro.getNro_expediente());
		parametros.put("nomCom", null);

		logger.debug(parametros);

		lstExpedientesModel = getExpedienteServicio().listarPaginacion(
				parametros);

		logger.debug(lstExpedientesModel.size());
		return SUCCESS;
	}

	@Action(value = "/verReporte", results = { @Result(params = {
			"contentType", "${extension}", "inputName", "documento",
			"contentDisposition", "inline; filename=\"${fileName}\"",
			"bufferSize", "1024" }, name = "success", type = "stream") })
	public String exportaReporte() throws JRException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("nroexpediente", registroSeleccionado.getNro_expediente());
		documento = new ByteArrayInputStream(exportReport(
				"rptExpedienteConsulta.jasper", params));
		fileName = "Reportillo";
		return SUCCESS;
	}

	public byte[] exportReport(String reportName, Map<String, Object> params)
			throws JRException {
		ServletContext context = ServletActionContext.getServletContext();
		String ruta = context.getRealPath(File.separator) + "dawi-fcs"
				+ File.separator + "recursos" + File.separator + "reportes"
				+ File.separator;
		JasperPrint jprint = JasperFillManager.fillReport(ruta + reportName,
				params, MyBatisUtil.getCurrentSession().getConnection());
		return JasperExportManager.exportReportToPdf(jprint);
	}

	@Override
	public String nuevo() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String editar() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Action(value = "/imprimirExpediente", results = { @Result(name = "success", location = "consulta_expediente_listado", type = "tiles") })
	public String ver() throws Exception {
		return SUCCESS;
	}

	@Override
	public String eliminar() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String inactivar() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String guardar() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String salir() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validar() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String prepararDatosMantenimiento() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String prepararDatosListado() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String inicializarFiltrosListado() throws Exception {
		filtro = new Expediente();
		return null;
	}

	@Override
	public String inicializarDatosMantenimiento() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Expediente getFiltro() {
		return filtro;
	}

	public void setFiltro(Expediente filtro) {
		this.filtro = filtro;
	}

	public DtoExpediente getRegistroSeleccionado() {
		return registroSeleccionado;
	}

	public void setRegistroSeleccionado(DtoExpediente registroSeleccionado) {
		this.registroSeleccionado = registroSeleccionado;
	}

	public List<DtoExpediente> getLstExpedientesModel() {
		return lstExpedientesModel;
	}

	public void setLstExpedientesModel(List<DtoExpediente> lstExpedientesModel) {
		this.lstExpedientesModel = lstExpedientesModel;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public InputStream getDocumento() {
		return documento;
	}

	public void setDocumento(InputStream documento) {
		this.documento = documento;
	}

}
